{extend name="common@base/common" /}

{block name="body"}
<style>
    .form-horizontal {
        display: none;
        padding: 20px;
    }
</style>
<div class="tools">
    <a class="btn" href="{:U('Attribute/add?model_id='.$model_id)}">新 增</a>
    <a class="btn" href="javascript:void(0)" onclick="addFormHas()">批量新增常用字段</a>
    {if condition="!input('?app_name')"}
    <a class="btn" href="{:U('model/edit?id='.$model_id)}#2">返回模型定义页</a>
    {/if}
    (下面的列表支持拖动排序)
</div>

<!-- 数据列表 -->
<div class="data-table">
    <div class="data-table table-striped">
        <form id="form" method="post">
            <input name="model_id" type="hidden" value="{$model_id}"/>
            <table>
                <thead>
                <tr>
                    <th>字段</th>
                    <th>名称</th>
                    <th>数据类型</th>
                    <th>字段定义</th>
                    <th>是否显示</th>
                    <th>动态显示</th>
                    <th>是否必填</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody class="needdragsort layui-form">
                {notempty name="list_data"}
                {php}$is_show_arr = array(0=>'不显示',1=>'始终显示',2=>'新增显示',3=>'编辑显示',5=>'条件控件',4=>'隐藏值');
                $is_must_arr = array(0=>'否',1=>'是');
                {/php}
                {volist name="list_data" id="vo"}
                {php}$vo['is_show'] = intval($vo['is_show']);$vo['is_must'] = intval($vo['is_must']);
                {/php}
                <tr>
                    <td><input name="sort[]" type="hidden" value="{$key}">{$key}</td>
                    <td><a href="{:U('Attribute/edit',['name'=>$key,'model_id'=>$model_id])}">{$vo.title}</a></td>
                    <td><span>{:get_attribute_type($vo['type'])}</span></td>
                    <td><span>{$vo['field']}</span></td>
                    <td><span>{$is_show_arr[$vo['is_show']]}</span></td>
                    <td><span>{$vo['show']}</span></td>
                    <td><input
                            url="{:U('switchAjaxUpdate',['table'=>'attribute','field'=>'is_must','name'=>$key,'model_id'=>$model_id])}"
                            {notempty name="vo.is_must" } checked {/notempty} type="checkbox" lay-skin="switch"
                        lay-text="是|否" lay-filter="switch-ajax-update" />
                    </td>
                    <td>

                        <a href="javascript:void(0)" onclick="showSet('{$key}')">动态显示</a>

                        <a href="{:U('Attribute/edit',['name'=>$key,'model_id'=>$model_id])}">编辑</a>
                        <a class="confirm" href="{:U('Attribute/remove',['name'=>$key,'model_id'=>$model_id])}">删除</a>

                    </td>
                </tr>
                {/volist}
                {else/}
                <td colspan="7" class="text-center"> aOh! 暂时还没有内容!</td>
                {/notempty}
                </tbody>
            </table>
        </form>
    </div>
</div>

<div id="addFormHas" class="form-horizontal">

    <div class="form-group">

        {volist name="field_lists" id="f"}
        <div class="checkbox">
            <input type="checkbox" class="regular-checkbox" value="{$key}" id="fields_{$key}" name="fields[]">
            <label for="fields_{$key}"></label>{$key}（{$f.title}）
        </div>
        {/volist}
    </div>
    <!-- 按钮 -->
    <div class="form-item cf">
        <label class="item-label"></label>
        <div class="controls edit_sort_btn">
            <button class="btn submit-btn" type="submit" target-form="form-horizontal" onclick="add()">确 定</button>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script src="__STATIC__/thinkbox/jquery.thinkbox.js?v={:SITE_VERSION}"></script>
<script type="text/javascript" src="__STATIC__/jquery.dragsort-0.5.2.min.js"></script>
<script type="text/javascript">


    $(function () {
        $("#search").click(function () {
            var url = $(this).attr('url');
            var status = $('select[name=status]').val();
            var search = $('input[name=search]').val();
            if (status != '') {
                url += '/status/' + status;
            }
            if (search != '') {
                url += '/search/' + search;
            }
            url = url.replace(/[?&=]/g, '/');
            window.location.href = url;
        });

        $(".needdragsort").dragsort({
            dragSelector: 'tr',
            dragSelectorExclude: 'a[href]',
            dragBetween: true,	//允许拖动到任意地方
            dragEnd: function () {
                doSave();
            }
        });


    })

    function doSave() {
        var url = "{:U('save_sort')}"
        var data = $('#form').serialize()
        $.post(url, data, function (res) {
            if (res.code == 0) {
                updateAlert('保存排序成功', 'alert-success');
            } else {
                updateAlert(res.msg);
            }
        })
    }

    function addFormHas() {
        layer.open({
            type: 1,
            content: $('#addFormHas')
        });
    }

    function showSet(field) {
        var url = "{:U('showSet',['only_body'=>1,'model_id'=>$model_id])}"
        layer.open({
            type: 2,
            area: ['600px', '600px'],
            content: url + "/field/" + field
        });
    }

    function add() {
        var fields = [];

        $('input[name="fields[]"]:checked').each(function () {
            fields.push($(this).val());
        })
        if (fields.length == 0) {
            updateAlert('请先选择字段');
            return false
        }
        var model_id = "{:input('model_id/d',-1)}";
        $.post("{:U('add_fields')}", {fields, model_id}, function (res) {
            if (res.code == 0) {
                window.location.href = res.url;
            } else {
                updateAlert(res.msg);
            }
        })
    }
</script>
{/block}
